package com.mes.cloud.jedis.util;

import com.mes.cloud.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description: redis工具类
 * @create: 2018-11-08 15:27
 **/
@Component
public class RedisUtil {
    /**
     * 默认过期时长，单位：秒
     */
    public static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public static long NOT_EXPIRE = -1;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;

    /**
     * 弹出元素并删除
     *
     * @param key 键
     * @return
     */
    public String popValue(String key) {
        return redisTemplate.opsForSet().pop(key).toString();
    }

    public void hDel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> hmList(String key, Class clazz) {
        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (null != map && map.size() > 0) {
            List list = new ArrayList<>();
            map.forEach((k, v) -> list.add(JsonUtil.Json2Java(String.valueOf(v), clazz)));
            return list;
        } else {
            return null;
        }
    }

    public void hSet(String key, String item, Object value) {
        redisTemplate.opsForHash().put(key, item, JsonUtil.Java2Json(value));
    }

    public <T> T hGet(String key, String item, Class<T> clazz) {
        Object obj = redisTemplate.opsForHash().get(key, item);
        return obj == null ? null : JsonUtil.Json2Java(String.valueOf(obj), clazz);
    }

    public Object hGet(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    public void set(String key, Object value, long expire) {
        valueOperations.set(key, JsonUtil.Java2Json(value), expire, TimeUnit.SECONDS);
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public void expire(String key, long expire) {
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : JsonUtil.Json2Java(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public <T> List<T> getObjectList(String key, Class clazz) {
        String value = valueOperations.get(key);
        return value == null ? null : JsonUtil.Json2JavaList(value, clazz);
    }

    public <T> List<T> getObjectList(String key, Class clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : JsonUtil.Json2JavaList(value, clazz);
    }

    public void set(String key, String hashKey, String hashValue) {
        stringRedisTemplate.opsForHash().put(key, hashKey, hashValue);
    }

    public String get(String key, String hashKey) {
        return (String) stringRedisTemplate.opsForHash().get(key, hashKey);
    }

    public boolean hasKey(String key, String hashKey) {
        return stringRedisTemplate.opsForHash().hasKey(key, hashKey);
    }

    public void delete(String key, String hashKey) {
        stringRedisTemplate.opsForHash().delete(key, hashKey);
    }

    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public boolean setScheduler(final String key, String value) {
        boolean result = false;
        result = valueOperations.setIfAbsent(key, value);
        return result;
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
